<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue</title>
  </head>
  <body>
  <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <div id="app">
      <input v-checklength="username" v-model="username">
    </div>
    <script>
      //检查内容的长度，如果小于6位变红，大于等于6位变绿
        Vue.directive('checklength',{
          bind(el){
            el.style.color ='gray';
          },
          componentUpdated(el,binding){
            let {value} = el;
            if(value.length<6){
              el.style.color ='red';
            }else{
              el.style.color ='green';
            }
          }
           
        });
        var vm = new Vue({
            el:'#app',
            data:{username:''}
        })
    </script>
  </body>
</html>
